﻿using System;
using System.Text;

namespace ScintillaNET_Editor_Plugin {

    public class plugin : Notepad_X.Plugin.IPlugin {
        public static LanguageManager lngMng;
        private static bool highlighters = true;
        public static string directory;

        public static bool Highlighters {
            get { return highlighters; }
            set {
                if (value) {
                    if (lngMng == null) {
                        lngMng = new LanguageManager();
                        lngMng.Scanfolder();
                    }
                } else {
                    lngMng = null;
                }
                highlighters = value;
                saveSettings();
             
            }
        }

        Notepad_X.Main main; // Notepad X Main form  

        public string Name {
            get { return "ScintillaNET Editor Plugin"; }
        }
        public string Author {
            get { return "NohponeX"; }
        }
        public string Description {
            get { return "With this plugin you can open & edit document using the Scintilla text editor control"; }
        }
        public string Version {
            get { return "0.99"; }
        }
        public bool HasSettings {
            get { return true ; }
        }
//        private bool isDefault = true;
     private int typeIndex;

        public void Initialize() {
            main = Notepad_X.Static.MainInstance;

            if (!System.IO.File.Exists(Notepad_X.Main.System_ROOT + "\\Plugins\\ScintillaNet.dll"))
                if (System.Windows.Forms.MessageBox.Show("\\Plugins\\ScintillaNet.dll is missing, Notepad X will try to download it automaticly contiue or you must fix it manually", "", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) {
                    Notepad_X.Download dll = new Notepad_X.Download("http://notepad-x.sourceforge.net/files/ScintillaNet.dll", Notepad_X.Main.System_ROOT + "\\Plugins\\ScintillaNet.dll", Notepad_X.DownloadTypes.Autoclose);
                    dll.ShowDialog(null);
                }


            for (int i = 0; i < Notepad_X.Main.app_settings.pluginsSettings.Count; i++) {
                if (Notepad_X.Main.app_settings.pluginsSettings[i].name.ToLower() == this.Name.Replace(" ", "").ToLower()) {
                    foreach (System.Collections.Generic.KeyValuePair<string, string> key in Notepad_X.Main.app_settings.pluginsSettings[i].values) {
                        if (key.Key.ToLower() == "highlighters") {
                            bool.TryParse(key.Value, out highlighters);
                            break;
                        }
                    }
                    break;

                }
            }
           typeIndex= Notepad_X.Classes.DocumentTypes.AddType(typeof(ScintillaNetDocument));


            main.system_menuItem_Get("Menu_Edit_Find").Click -= new EventHandler(main.handler_find);
            main.system_menuItem_Get("Menu_Edit_Find").Click += new EventHandler(find_Click);

            main.system_menuItem_Get("Menu_Edit_Replace").Click -= new EventHandler(main.handler_replace);
            main.system_menuItem_Get("Menu_Edit_Replace").Click += new EventHandler(replace_Click);

            directory = Notepad_X.Main.System_ROOT + "\\Plugins\\ScintillaNet\\";

            if (highlighters) {
                lngMng = new LanguageManager();

                lngMng.Scanfolder();
            }
        }
        public void Settings() {

            //Notepad_X.Components.TabPageX newTab = new Notepad_X.Components.TabPageX();
            //System.Windows.Forms.TabPage newTab = new System.Windows.Forms.TabPage(this.Name + " Settings");
            //System.Windows.Forms.TextBox n = new System.Windows.Forms.TextBox();
            //newTab.Controls.Add(n);
            //main.Panel.TabPages.Add(newTab);
            new SettingsDialog().Show(main);
        }
        void find_Click(object sender, EventArgs e) {
            int id = main.get_selectedID();
            if (id != -1) {
                if (main.Documents[id].GetType() == typeof(ScintillaNetDocument)) {
                    main.Documents[id].find(null);
                }else{
                    main.handler_find(null, null);
                }
            }
        }

        void replace_Click(object sender, EventArgs e) {
            int id = main.get_selectedID();
            if (id != -1) {
                if (main.Documents[id].GetType() == typeof(ScintillaNetDocument)) {
                    main.Documents[id].replace(null,null);
                } else {
                    main.handler_replace(null, null);
                }
            }
        }
        private static void saveSettings() {
            bool found = false;
            for (int i = 0; i < Notepad_X.Main.app_settings.pluginsSettings.Count; i++) {
                if (Notepad_X.Main.app_settings.pluginsSettings[i].name.ToLower() == ("ScintillaNET Editor Plugin").Replace(" ", "").ToLower()) {
                    for (int j = 0; j < Notepad_X.Main.app_settings.pluginsSettings[i].values.Count; j++) {
                        if (Notepad_X.Main.app_settings.pluginsSettings[i].values[j].Key.ToLower() == "highlighters") {
                            //found j
                            found = true;
                            Notepad_X.Main.app_settings.pluginsSettings[i].values[j] = new System.Collections.Generic.KeyValuePair<string, string>("highlighters", highlighters.ToString());
                            break;
                        }
                    }
                    //End of found i
                    if (!found) {
                        Notepad_X.Main.app_settings.pluginsSettings[i].values.Add(new System.Collections.Generic.KeyValuePair<string, string>("highlighters", highlighters.ToString()));
                        found = true;
                        break;
                    }
                }
            }
            //add new
            if (!found) {
                Notepad_X.Classes.pluginSetting pS = new Notepad_X.Classes.pluginSetting(("ScintillaNET Editor Plugin").Replace(" ", ""));

                pS.values.Add(new System.Collections.Generic.KeyValuePair<string, string>("highlighters", highlighters.ToString()));
                Notepad_X.Main.app_settings.pluginsSettings.Add(pS);
            }
        }
        public void Dispose() {


            Notepad_X.Classes.DocumentTypes.RemoveType(typeIndex);


            main.system_menuItem_Get("Menu_Edit_Find").Click += new EventHandler(main.handler_find);
            main.system_menuItem_Get("Menu_Edit_Find").Click -= new EventHandler(find_Click);

            main.system_menuItem_Get("Menu_Edit_Replace").Click += new EventHandler(main.handler_replace);
            main.system_menuItem_Get("Menu_Edit_Replace").Click -= new EventHandler(replace_Click);
        }    
    }
}
